package com.stock.stockana.requests;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetAllStocksEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * ---------------------------------------------------
 * Description: 获取用户所有的stock的请求类
 * Author: ljgsonx
 * Belong to: com.wds.stock.stockana.requests
 * Time: 2016/1/15 17:42
 * ---------------------------------------------------
 */
public class GetAllStocks extends BaseStringRequest<GetAllStocks> {

    private final static String TAG = "GetAllStocks--->";
    public static List<StockInfoBean> stocks = new ArrayList<>();
    public static StockInfoBean[] fixedStock = new StockInfoBean[3];

    private GetAllStocks(){
        super(Method.GET, NetWorkConstants.GET_ALLINVOICES_URL, listener, errorListener);
        Log.d(TAG, NetWorkConstants.GET_ALLINVOICES_URL);
    }

    public static GetAllStocks build() {
        return new GetAllStocks();
    }

    static Response.Listener<String> listener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response) {
            if("[".charAt(0) == response.charAt(0)) {
                List<StockInfoBean> tempStocks = JSON.parseArray(response, StockInfoBean.class);
                StockInfoBean sb1 = null;
                StockInfoBean sb2 = null;
                StockInfoBean sb3 = null;
                for (int i = 0; i < tempStocks.size(); i++) {
                    if ("SH000001".equals(tempStocks.get(i).getStNum())) {
                        fixedStock[0] = tempStocks.get(i);
                        sb1 = tempStocks.get(i);
                    } else if ("SZ399001".equals(tempStocks.get(i).getStNum())) {
                        fixedStock[1] = tempStocks.get(i);
                        sb2 = tempStocks.get(i);
                    } else if ("SZ399006".equals(tempStocks.get(i).getStNum())) {
                        fixedStock[2] = tempStocks.get(i);
                        sb3 = tempStocks.get(i);
                    }
                }
                if(sb1 != null) {
                    tempStocks.remove(sb1);
                }
                if(sb2 != null) {
                    tempStocks.remove(sb2);
                }
                if(sb3 != null) {
                    tempStocks.remove(sb3);
                }
                stocks.clear();
                stocks.addAll(tempStocks);
                EventBus.getDefault().post(new GetAllStocksEvent(NetWorkConstants.RESPONSE_CODE_OK, null));
            }else if("{".charAt(0) == response.charAt(0)){
                ResponseBean rb = JSON.parseObject(response, ResponseBean.class);
                EventBus.getDefault().post(new GetAllStocksEvent(NetWorkConstants.RESPONSE_CODE_ERROR,NetWorkConstants.RESPONSE_ERROR2 +rb.getErrorMsg()));
            }else{
                EventBus.getDefault().post(new GetAllStocksEvent(NetWorkConstants.RESPONSE_CODE_ERROR,NetWorkConstants.RESPONSE_ERROR1 + response));
            }
        }
    };

    static Response.ErrorListener errorListener = new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            EventBus.getDefault().post(new GetAllStocksEvent(NetWorkConstants.RESPONSE_CODE_ERROR, NetWorkConstants.RESPONSE_ERROR3 + error.toString()));
        }
    };
}
